<?


class CTRLcompany extends CONTROL {

	function __construct( $vars ) {
		global $Object;
		
		$this->_addvars( $vars );

		if( $this->action != "list" )
			$this->object = $Object = $this->create_object();
		
	}

#	Edit Object.
	function filter_edit() {
		global $Engine, $Object;
		
		// define type
		$type = array_pop( explode( ".", $this->vars->type ));
		
		// check for uptype 
		if ($type == 'img' ) $uptype = 'media';
		else $uptype = $type;
			
		// return group
		return "
		ref.seed([
			new Element( 'h1' ).update( \"Beheer {$this->vars->type} '". (( $Object->get( "nameid" ) )? $Engine->text->get( $this->object->get( "nameid" ) ) : $this->object->id ) . "'\" ),
			new Textfield({ descr:'name {$this->vars->type}', type:'{$this->vars->type}', parentid:{$this->object->id}, textid:". $this->object->get( "nameid" ) .", values:". json_encode( $Engine->text->filter( $this->object->get( "nameid" ) )) ." }),
			new Htmlfield({ descr:'text {$this->vars->type}', type:'{$this->vars->type}', parentid:{$this->object->id}, textid:". $this->object->get( "textid" ) .", values:". json_encode( $Engine->text->filter($this->object->get( "textid" ), 'long' )) ." }),
			box = new Dropbox({ descr:'Beelden', type:'media." . $type . "', parenttype:'{$this->vars->type}', parentid:'{$this->object->id}', action:'win', blank:'add " . $type . "', id:{$this->object->id}, uptype:'" . $uptype . "', floats:". $this->filter_media( 'media.' . $type . '' ) ." }),
			new Options({ descr:'Opties', floats: [
				new Shift({ type:'{$this->vars->type}', descr:'Is public', value:" . $this->object->get( 'active' ) . ", field:'active', objid:" . $this->object->id ."}),
				new Rank({ type:'{$this->vars->type}', descr:'Rank', value:" . $this->object->get( 'rank' ) . ", field:'rank', objid:" . $this->object->id ." })
			]})
		]);";

	}
	
#	Render media floats
	function filter_media( $objtype ) {
		global $DB, $Object;
		
		$result = array();
		
		// print_r($Object->media->filter( $objtype ));
		
		if( $images = $Object->media->filter( $objtype ))
			foreach( $images as $img ) {
				$result[] = array( "id"=> $img['ID'], "type"=> $objtype, "name"=> $objtype, "list"=> array( $img['ID'], json_decode( $img[ 'meta' ])->raw ));
			}
		
		return json_encode( $result );
	}

}



